import { ISchema } from '@formily/react'

// 为ClientAgreementForm创建专门的配置schema，优化表达式编辑体验
export const ClientAgreementFormSettings: ISchema = {
  type: 'object',
  properties: {
    // 基础配置
    title: {
      type: 'string',
      'x-decorator': 'FormItem',
      'x-component': 'Input',
      title: '组件标题',
    },
    // 客户协议字段配置
    agreementName: {
      type: 'object',
      title: '客户协议字段',
      'x-decorator': 'FormItem',
      properties: {
        title: {
          type: 'string',
          'x-decorator': 'FormItem',
          'x-component': 'Input',
          title: '字段标题',
          default: '关联客户协议',
        },
        addonAfter: {
          type: 'string',
          'x-decorator': 'FormItem',
          'x-component': 'Input',
          title: '后缀按钮文本',
          default: '查询',
        },
        addonAfterDialog: {
          type: 'string',
          'x-decorator': 'FormItem',
          'x-component': 'Input',
          title: '对话框名称',
          default: '客户协议',
        },
        addonAfterClickExpression: {
          type: 'string',
          'x-decorator': 'FormItem',
          'x-component': 'ExpressionDialogSetter',
          title: '点击事件表达式',
          'x-component-props': {
            title: '编辑点击事件表达式',
            description: '定义点击查询按钮时的行为逻辑',
          },
        },
        visibleExpression: {
          type: 'string',
          'x-decorator': 'FormItem',
          'x-component': 'ExpressionDialogSetter',
          title: '显示条件表达式',
          'x-component-props': {
            title: '编辑显示条件',
            description: '定义字段的显示/隐藏条件',
          },
        },
      },
    },
    // 客户字段配置
    customerName: {
      type: 'object',
      title: '客户字段',
      'x-decorator': 'FormItem',
      properties: {
        title: {
          type: 'string',
          'x-decorator': 'FormItem',
          'x-component': 'Input',
          title: '字段标题',
          default: '关联客户',
        },
        addonAfter: {
          type: 'string',
          'x-decorator': 'FormItem',
          'x-component': 'Input',
          title: '后缀按钮文本',
          default: '查询',
        },
        addonAfterClickExpression: {
          type: 'string',
          'x-decorator': 'FormItem',
          'x-component': 'ExpressionDialogSetter',
          title: '点击事件表达式',
          'x-component-props': {
            title: '编辑点击事件表达式',
            description: '定义点击查询按钮时的行为逻辑，包含验证逻辑',
          },
        },
      },
    },
    // 对话框配置
    dialogs: {
      type: 'object',
      title: '对话框配置',
      'x-decorator': 'FormItem',
      properties: {
        customerDialog: {
          type: 'object',
          title: '客户对话框',
          'x-decorator': 'FormItem',
          properties: {
            title: {
              type: 'string',
              'x-decorator': 'FormItem',
              'x-component': 'Input',
              title: '对话框标题',
              default: '客户',
            },
            width: {
              type: 'string',
              'x-decorator': 'FormItem',
              'x-component': 'Input',
              title: '对话框宽度',
              default: '1200',
            },
            confirmExpression: {
              type: 'string',
              'x-decorator': 'FormItem',
              'x-component': 'ExpressionDialogSetter',
              title: '确认事件表达式',
              'x-component-props': {
                title: '编辑确认事件',
                description: '定义对话框确认按钮的行为',
              },
            },
          },
        },
        agreementDialog: {
          type: 'object',
          title: '客户协议对话框',
          'x-decorator': 'FormItem',
          properties: {
            title: {
              type: 'string',
              'x-decorator': 'FormItem',
              'x-component': 'Input',
              title: '对话框标题',
              default: '客户协议',
            },
            width: {
              type: 'string',
              'x-decorator': 'FormItem',
              'x-component': 'Input',
              title: '对话框宽度',
              default: '1200',
            },
            confirmExpression: {
              type: 'string',
              'x-decorator': 'FormItem',
              'x-component': 'ExpressionDialogSetter',
              title: '确认事件表达式',
              'x-component-props': {
                title: '编辑确认事件',
                description: '定义对话框确认按钮的行为',
              },
            },
          },
        },
      },
    },
    // API配置
    apiConfig: {
      type: 'object',
      title: 'API配置',
      'x-decorator': 'FormItem',
      properties: {
        customerApiPath: {
          type: 'string',
          'x-decorator': 'FormItem',
          'x-component': 'Input',
          title: '客户API路径',
          default: '/api/tenant/agreement/customer/list',
        },
        agreementApiPath: {
          type: 'string',
          'x-decorator': 'FormItem',
          'x-component': 'Input',
          title: '协议API路径',
          default: '/api/tenant/agreement/listAgreement',
        },
        requestMethod: {
          type: 'string',
          enum: ['GET', 'POST', 'PUT', 'DELETE'],
          'x-decorator': 'FormItem',
          'x-component': 'Select',
          title: '请求方法',
          default: 'POST',
        },
      },
    },
  },
}

export const ClientAgreementForm: ISchema & {
  AgreementDialog?: ISchema
  CustomerDialog?: ISchema
} = {
  type: 'void',
  'x-component': 'FormLayout',
  'x-component-props': {},
  'x-designable-id': 'a7lc6rticoa',
  'x-index': 0,
  properties: {
    agreementName: {
      type: 'string',
      title: '关联客户协议',
      'x-decorator': 'FormItem',
      'x-component': 'Input',
      'x-validator': [],
      'x-component-props': {
        addonBefore: '',
        addonAfter: '查询',
        addonAfterDialog: '客户协议',
        addonBeforeClickExpression: '',
        addonAfterClickExpression:
          "{{(ctx, deps) => {\r\n  const useDialogInstances = deps.useDialogInstances\r\n  const dialog = useDialogInstances().get('客户协议')\r\n  dialog.open(ctx)\r\n}}}",
      },
      'x-decorator-props': {},
      'x-designable-id': '2yw1dprzsxe',
      'x-index': 0,
      name: 'agreementName',
      required: true,
      'x-pattern': 'disabled',
      'x-reactions': {
        dependencies: [
          {
            property: 'value',
            type: 'any',
          },
        ],
        fulfill: {
          run: '',
          state: {
            visible:
              '{{JSON.parse(localStorage.getItem("adminQiankunGlobalState")).data.isMultiAgreet}}',
          },
        },
      },
    },
    customerName: {
      type: 'string',
      title: '关联客户',
      'x-decorator': 'FormItem',
      'x-component': 'Input',
      'x-validator': [],
      'x-component-props': {
        addonBefore: '',
        addonAfter: '查询',
        addonAfterClickExpression:
          "{{(ctx, deps) => {\r\n  const agreementName = ctx.form.getValuesIn('agreementName')\r\n  const {isMultiAgreet} = deps.getStorage('adminQiankunGlobalState') || {}\r\n  if (isMultiAgreet && !agreementName) {\r\n    deps.antdMsg(ctx.createT('请选择客户协议'), 'error')\r\n    return\r\n  }\r\n  const useDialogInstances = deps.useDialogInstances\r\n  const dialog = useDialogInstances().get('客户')\r\n  dialog.open(ctx)\r\n}}}",
      },
      'x-decorator-props': {},
      name: 'customerName',
      required: true,
      'x-pattern': 'disabled',
      'x-designable-id': '54snaqfi1l3',
      'x-index': 1,
    },
    sa4zrr7uokj: {
      type: 'void',
      'x-component': 'Dialog',
      'x-component-props': {
        dialogName: '客户',
        maskStyle: {},
        bodyStyle: {},
        style: {},
        okButtonProps: {},
        cancelButtonProps: {},
        title: '客户',
        width: '1200',
        confirm: '{{(ctx, deps) => {\r\n  console.log(ctx)\r\n}}}',
      },
      'x-designable-id': 'sa4zrr7uokj',
      'x-index': 2,
      properties: {
        umy58vpf3fl: {
          type: 'void',
          'x-component': 'ProTable',
          'x-component-props': {
            columns: {
              data: [
                {
                  title: '客户编号',
                  dataIndex: 'code',
                  ellipsis: true,
                  search: true,
                },
                {
                  title: '客户名称',
                  dataIndex: 'name',
                  ellipsis: true,
                  search: true,
                },
                {
                  title: '所属国家',
                  dataIndex: 'countryName',
                  ellipsis: true,
                  search: false,
                },
                {
                  title: '状态',
                  dataIndex: 'statusDesc',
                  ellipsis: true,
                  search: false,
                  width: 120,
                },
              ],
            },
            action: {
              buttons: {
                data: [],
              },
            },
            apiPath: 'api',
            rowKey: 'code',
            toolBarRender: {
              data: [],
            },
            rowSelection: {
              enabled: true,
              type: 'radio',
              onChange:
                '{{(ctx, deps) => {\r\n  const { selectedRows, form } = ctx\r\n  const { name, code } = selectedRows[0] || {}\r\n  form.setValues({\r\n    agreementCode: code,\r\n    agreementName: name\r\n  })\r\n}}}',
            },
            pagination: {},
            scroll: {
              x: 1100,
            },
            request:
              "{{async (ctx, deps) => {\r\n  const { request } = deps\r\n  const { params } = ctx\r\n  const res = await request({\r\n    uri: '/api/tenant/agreement/customer/list',\r\n    params: {\r\n      ...params,\r\n    },\r\n    method: 'POST',\r\n  })\r\n  return res\r\n}}}",
          },
          'x-designable-id': 'umy58vpf3fl',
          'x-index': 0,
        },
      },
    },
    '8ogk983918r': {
      type: 'void',
      'x-component': 'Dialog',
      'x-component-props': {
        dialogName: '客户协议',
        maskStyle: {},
        bodyStyle: {},
        style: {},
        okButtonProps: {},
        cancelButtonProps: {},
        title: '客户协议',
        width: '1200',
        confirm: '{{(ctx, deps) => {\r\n  console.log(ctx)\r\n}}}',
      },
      'x-designable-id': '8ogk983918r',
      'x-index': 3,
      properties: {
        pj3wkd090je: {
          type: 'void',
          'x-component': 'ProTable',
          'x-component-props': {
            columns: {
              data: [
                {
                  title: '协议编号',
                  dataIndex: 'code',
                  ellipsis: true,
                  search: false,
                },
                {
                  title: '客户编号',
                  dataIndex: 'cusCode',
                  ellipsis: true,
                  search: false,
                },
                {
                  title: '协议名称',
                  dataIndex: 'name',
                  ellipsis: true,
                  search: false,
                },
                {
                  title: '协议客户',
                  dataIndex: 'cusName',
                  ellipsis: true,
                  search: false,
                },
                {
                  title: '有效期',
                  dataIndex: 'updateTime',
                  ellipsis: true,
                  search: false,
                  rangStartName: 'updateTimeStart',
                  rangEndName: 'updateTimeEnd',
                  isRender: true,
                  render:
                    '{{(ctx) => {\r\n  const record = ctx.record\r\n  return record.startDate && record.endDate && `${record.startDate}~${record.endDate}`\r\n}}}',
                  width: 200,
                },
                {
                  title: '状态',
                  dataIndex: 'statusName',
                  ellipsis: true,
                  search: false,
                  width: 120,
                },
                {
                  title: '客户信息',
                  search: true,
                  hideInTable: true,
                },
                {
                  title: '协议信息',
                  search: true,
                  hideInTable: true,
                },
              ],
            },
            action: {
              buttons: {
                data: [],
              },
            },
            apiPath: 'api',
            rowKey: 'code',
            toolBarRender: {
              data: [],
            },
            rowSelection: {
              enabled: true,
              type: 'radio',
              onChange:
                '{{(ctx, deps) => {\r\n  const { selectedRows, form } = ctx\r\n  const { name, code } = selectedRows[0] || {}\r\n  form.setValues({\r\n    agreementCode: code,\r\n    agreementName: name\r\n  })\r\n}}}',
            },
            pagination: {},
            scroll: {
              x: 1100,
            },
            request:
              "{{async (ctx, deps) => {\r\n  const { request } = deps\r\n  const { params } = ctx\r\n  const res = await request({\r\n    uri: '/api/tenant/agreement/listAgreement',\r\n    params: {\r\n      ...params,\r\n    },\r\n    method: 'POST',\r\n  })\r\n  return res\r\n}}}",
          },
          'x-designable-id': 'pj3wkd090je',
          'x-index': 0,
        },
      },
    },
  },
}

export default ClientAgreementForm
